<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$limit = 50;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    die('stop');
  }

  $keys = $redis->hKeys(MH_PIDS);
  foreach ($keys as $pid) {
    $result = shell_exec('ps aux | grep -v " grep"| grep [[:space:]]'.$pid.'[[:space:]] | wc -l');
    $result = intval(trim($result));
    //进程已退出
    if($result == 0) {
      $redis->hDel(MH_PIDS, $pid);
    }
    //超过6个小时的kill
    else {
      $item = $redis->hGet(MH_PIDS, $pid);
      list($time, $file) = explode('||', $item);
      $time = strtotime($time);
      $now  = time();
      if($now - $time > 3600*2) {
        shell_exec('kill -9 '.$pid);
        $redis->hDel(MH_PIDS, $pid);
      }
    }
    //var_dump($result);
  }

  //sleep(1);
  exit;
}